package com.tuanzi.loan.business.repository.family.impl;

import com.base.common.log.LogService;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.business.entity.family.LoanWholeSaleComment;
import com.tuanzi.loan.business.repository.family.custom.LoanWholeSaleCommentRepositoryCustom;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.LoanWholeSaleCommentSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanWholeSaleCommentSearchResponse;
import com.tuanzi.loan.vo.view.family.CommRegionNewInfo;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_WHOLESALE_COMMENT;

public class LoanWholeSaleCommentRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanWholeSaleCommentRepositoryCustom {
    private SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");
    @Autowired
    DictionaryService service;

    @Override
    protected TableImpl getTable() {
        return LOAN_WHOLESALE_COMMENT;
    }
    @Override
    public Page<LoanWholeSaleCommentSearchResponse> list(LoanWholeSaleCommentSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_WHOLESALE_COMMENT.fields());
        eq(LOAN_WHOLESALE_COMMENT.OWNERNAME,searchRequest.getOwnername());
        if (searchRequest.getStatus()!=null||"".equals(searchRequest.getStatus())){
            eq(LOAN_WHOLESALE_COMMENT.STATUS,searchRequest.getStatus());
        }
        eq(LOAN_WHOLESALE_COMMENT.STATUS,searchRequest.getStatus());
        orderBy(LOAN_WHOLESALE_COMMENT.SORT, Sort.Direction.ASC);
        return search(pageable,record -> {
            LoanWholeSaleCommentSearchResponse response = new LoanWholeSaleCommentSearchResponse();
            response.setOwnername(record.get(LOAN_WHOLESALE_COMMENT.OWNERNAME));
            response.setSort(record.get(LOAN_WHOLESALE_COMMENT.SORT));
            response.setType(record.get(LOAN_WHOLESALE_COMMENT.TYPE));
            response.setNumber(record.get(LOAN_WHOLESALE_COMMENT.NUMBER));
            response.setAmount(record.get(LOAN_WHOLESALE_COMMENT.AMOUNT));
            response.setApprovenum(record.get(LOAN_WHOLESALE_COMMENT.APPROVENUM));
            response.setAvgprice(record.get(LOAN_WHOLESALE_COMMENT.AVGPRICE));
            response.setCooperate(record.get(LOAN_WHOLESALE_COMMENT.COOPERATE));
            try {
                response.setDowntime(SDF.parse(record.get(LOAN_WHOLESALE_COMMENT.DOWNTIME)+":00"));
                response.setUppertime(SDF.parse(record.get(LOAN_WHOLESALE_COMMENT.UPPERTIME)+":59"));
            } catch (ParseException e) {
                LogService.error("日期转换异常",e);
            }
            response.setUpdatetime(record.get(LOAN_WHOLESALE_COMMENT.UPDATE_TIME));
            response.setStatus(record.get(LOAN_WHOLESALE_COMMENT.STATUS));
            response.setEditor(record.get(LOAN_WHOLESALE_COMMENT.UPDATOR));
            response.setOnlinedate(record.get(LOAN_WHOLESALE_COMMENT.ONLINEDATE));
            response.setId(record.get(LOAN_WHOLESALE_COMMENT.ID));

            response.setDaikuan_amount(record.get(LOAN_WHOLESALE_COMMENT.DAIKUAN_AMOUNT));
            response.setIncome(record.get(LOAN_WHOLESALE_COMMENT.INCOME));
            response.setHouse_fund(record.get(LOAN_WHOLESALE_COMMENT.HOUSE_FUND));
            response.setSocial_insurance(record.get(LOAN_WHOLESALE_COMMENT.SOCIAL_INSURANCE));
            response.setHas_car(record.get(LOAN_WHOLESALE_COMMENT.HAS_CAR));
            response.setHas_estate(record.get(LOAN_WHOLESALE_COMMENT.HAS_ESTATE));
            response.setCredit(record.get(LOAN_WHOLESALE_COMMENT.CREDIT));
            response.setHas_weilidai(record.get(LOAN_WHOLESALE_COMMENT.HAS_WEILIDAI));
            response.setLife_insurance(record.get(LOAN_WHOLESALE_COMMENT.LIFE_INSURANCE));
            response.setComm_regoin(record.get(LOAN_WHOLESALE_COMMENT.COMM_REGOIN));
            response.setSocial_insuranceduration(record.get(LOAN_WHOLESALE_COMMENT.SOCIAL_INSURANCEDURATION));
            response.setHouse_fundduration(record.get(LOAN_WHOLESALE_COMMENT.HOUSE_FUNDDURATION));
            return  response;
        });
    }

    @Override
    public Integer updateLoanWholeSaleComment(LoanWholeSaleComment request) {
        String uppertime =request.getUppertime();
        String downtime = request.getDowntime();
        if (uppertime==null){
            uppertime="00:00";
        }
        if (downtime==null){
            downtime="23:59";
        }
        LoginUser currentUser = CurrentUser.get();
        return dslContext.update(LOAN_WHOLESALE_COMMENT)
                .set(LOAN_WHOLESALE_COMMENT.OWNERNAME,request.getOwnername())
                .set(LOAN_WHOLESALE_COMMENT.SORT,request.getSort())
                .set(LOAN_WHOLESALE_COMMENT.NUMBER,request.getNumber())
                .set(LOAN_WHOLESALE_COMMENT.UPPERTIME, uppertime)
                .set(LOAN_WHOLESALE_COMMENT.DOWNTIME,downtime)
                .set(LOAN_WHOLESALE_COMMENT.AVGPRICE,request.getAvgprice())
                .set(LOAN_WHOLESALE_COMMENT.AMOUNT,request.getAmount())
                .set(LOAN_WHOLESALE_COMMENT.APPROVENUM,request.getApprovenum())
                .set(LOAN_WHOLESALE_COMMENT.COOPERATE,request.getCooperate())
                .set(LOAN_WHOLESALE_COMMENT.UPDATOR, null == currentUser ? request.getUpdator() : currentUser.getName())
                .set(LOAN_WHOLESALE_COMMENT.UPDATE_TIME, Timestamp.valueOf(DateUtils.getCurrentTime()))
                .set(LOAN_WHOLESALE_COMMENT.ONLINEDATE,request.getOnlinedate())
                .set(LOAN_WHOLESALE_COMMENT.HAS_WEILIDAI,request.getHas_weilidai())
                .set(LOAN_WHOLESALE_COMMENT.HAS_ESTATE,request.getHas_estate())
                .set(LOAN_WHOLESALE_COMMENT.HAS_CAR,request.getHas_car())
                .set(LOAN_WHOLESALE_COMMENT.CREDIT,request.getCredit())
                .set(LOAN_WHOLESALE_COMMENT.SOCIAL_INSURANCE,request.getSocial_insurance())
                .set(LOAN_WHOLESALE_COMMENT.SOCIAL_INSURANCEDURATION,request.getSocial_insuranceduration())
                .set(LOAN_WHOLESALE_COMMENT.HOUSE_FUNDDURATION,request.getHouse_fundduration())
                .set(LOAN_WHOLESALE_COMMENT.HOUSE_FUND,request.getHouse_fund())
                .set(LOAN_WHOLESALE_COMMENT.LIFE_INSURANCE,request.getLife_insurance())
                .set(LOAN_WHOLESALE_COMMENT.DAIKUAN_AMOUNT,request.getDaikuan_amount())
                .set(LOAN_WHOLESALE_COMMENT.INCOME,request.getIncome())
                .set(LOAN_WHOLESALE_COMMENT.COMM_REGOIN,request.getComm_regoin())
                .set(LOAN_WHOLESALE_COMMENT.MINAGE,request.getMinage())
                .set(LOAN_WHOLESALE_COMMENT.MAXAGE,request.getMaxage())
                .set(LOAN_WHOLESALE_COMMENT.AMOUNT,request.getAmount())
                .set(LOAN_WHOLESALE_COMMENT.WAITUI,request.getWaitui())
                .set(LOAN_WHOLESALE_COMMENT.ZHANNEI,request.getZhannei())
                .set(LOAN_WHOLESALE_COMMENT.STATUS,request.getStatus())
                .set(LOAN_WHOLESALE_COMMENT.CHECK_ID, request.getCheckId())
                .set(LOAN_WHOLESALE_COMMENT.FILTER_CHANNEL, request.getFilterChannel())
                .set(LOAN_WHOLESALE_COMMENT.CITY_LIMIT, request.getCityLimit())
                .set(LOAN_WHOLESALE_COMMENT.TOTAL_LIMIT, request.getTotalLimit())
                .where(LOAN_WHOLESALE_COMMENT.TYPE.eq(request.getType()))
                .execute();
    }

    @Override
    public List<CommRegionNewInfo> findAllRegionInfo() {

        List<CommRegionNewInfo> regin = dslContext.fetch(
                "select * from comm_region_new where level=2 order by id asc").map(new RecordMapper<Record, CommRegionNewInfo>() {

            @Override
            public CommRegionNewInfo map(Record record) {
                CommRegionNewInfo comm = new CommRegionNewInfo();
                comm.setId(record.get("id",Long.class));
                comm.setName(record.get("name",String.class));
                return comm;
            }
        });
        return regin;
    }
}
